﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ECRMS.WinForm.BaseClass;
using ECRMS.WinForm.WinService;
using System.Linq;
using ECRMS.WinForm.BaseClass.Common;
using ECRMS.WinForm.Properties;
using DevExpress.XtraEditors.Controls;
using ECRMS.WinForm.BaseClass.Other;

namespace ECRMS.WinForm.ModuleEdits
{
    public partial class ReportEdit : BaseEdit
    {
        public ReportEdit(Rectangle r, OperationEnum op)
        {
            InitializeComponent();
            InitView(r, op);
            InitEmployeeCombox();
            InitData();
        }

        private void InitEmployeeCombox()
        {
            FuncResult result = core.CoreEmployee.GetEmployees(core.CoreUser.UserInfo.CorporationRefId);
            if (result.Status == 0)
            {
                XtraMessageBox.Show(this, Language_ch.ResourceManager.GetString(result.Message));
                return;
            }
            ComboBoxItemCollection coll = cbe_user.Properties.Items;
            coll.BeginUpdate();
            coll.Clear();

            coll.Add(new ComboxData { Text = "", GuidValue = Guid.Empty });

            foreach (CtEmployee item in core.CoreEmployee.Employees)
            {
                coll.Add(new ComboxData
                {
                    Text = item.UserInfo.Name,
                    GuidValue = item.UserInfo.UserRefId
                });
            }
            coll.EndUpdate();
        }

        private void InitData()
        {
            switch (base.Op)
            {
                case OperationEnum.New:
                    txt_name.Text = "";
                    cbe_user.SelectedIndex = 0;
                    break;
                case OperationEnum.Modify:
                    txt_name.Text = core.CoreReportOfPlan.OperatorRow.Name;
                    cbe_user.SelectedIndex = core.CoreCommon.GetComboxIndex(cbe_user, core.CoreReportOfPlan.OperatorRow.AssignedUserRefId);
                    break;
                default:
                    break;
            }
        }

        private void InitView(Rectangle r, OperationEnum op)
        {
            this.Text = "报表模块编辑";
            this.Left = r.Left + (r.Width - this.Width) / 2;
            this.Top = r.Top + (r.Height - this.Height) / 2;

            base.NewBarButtonVisiable = false;
            base.SaveBarButtonVisiable = true;
            base.EditBarButtonVisiable = false;
            base.DelBarButtonVisiable = false;
            base.RefreshBarButtonVisiable = false;
            base.ResetBarButtonVisiable = true;
            base.OtherBarButtonVisiable = false;
            base.Other1ButtonVisiable = false;
            base.Op = op;
        }
        //保存
        public override void saveBarButtonClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            base.saveBarButtonClick(sender, e);
            core.CoreReportOfPlan.OperatorRow.Name = txt_name.Text.Trim();
            Guid employeeId = (cbe_user.SelectedItem as ComboxData).GuidValue;
            //if (employeeId == Guid.Empty)
            //{
            //    core.CoreReport.OperatorRow.AssignedUserRefId = null;
            //}
            core.CoreReportOfPlan.OperatorRow.AssignedUserRefId = employeeId;

            FuncResult result = core.CoreReportOfPlan.UpdateReportNoContent();
           
            if (result.Status == 0)
            {
                XtraMessageBox.Show(this, Language_ch.ResourceManager.GetString(result.Message));
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();

        }
        //重置
        public override void resetBarButtonClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            base.resetBarButtonClick(sender, e);
            InitData();
        }
    }
}